package chapter7;

import java.io.File;

/**
 * 
 * @author Lin...BC
 * 
 */
public class SearchJava {

	Object[] value;

	float inc;

	int size;

	int index;

	public SearchJava() {

		index = 0;

		size = 10;

		inc = 0.5f;

		value = new Object[size];
	}

	/**
	 * To insert a name into array
	 * 
	 * @param name
	 *            The name insert into array
	 */
	public void addArray(String name) {
		index++;

		if (index >= size) {

			size += size * inc;

			Object[] tmp = new Object[size];

			System.arraycopy(value, 0, tmp, 0, value.length);

			value = tmp;
		}

		value[index] = name;

	}

	/**
	 * Search files of same class
	 * 
	 * @param dir
	 *            The index you want to search
	 */

	public void searchJavaTest(String dir) {

		File f1 = new File(dir);

		File[] f2 = f1.listFiles();

		if (f2 != null) {

			for (int i = 0; i < f2.length; i++) {

				if (f2[i] != null) {

					File tmp = f2[i];

					if (tmp.isFile()) {

						String a = tmp.getAbsolutePath();

						if (a.endsWith("java")) {

							addArray(a);
						}
					} else if (tmp.isDirectory()) {

						String a = tmp.getAbsolutePath();

						searchJavaTest(a);
					}
				}
			}
		}
	}

	public static void main(String[] args) {

		SearchJava sj = new SearchJava();

		System.out.println("1:Search c:");

		System.out.println("2:Search d:");

		System.out.println("3:Search e:");

		sj.searchJavaTest("e:\\");

		for (int i = 0; i < sj.value.length; i++) {

			if (sj.value[i] != null) {

				System.out.println(sj.value[i]);
			}
		}
		System.out.println(sj.value.length);
	}
}
